package com.sdp.right.control;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.right.cache.clean.RightCleanCache;
import com.sdp.right.db.sys_dict_item.service.ISysDictItemService;
import com.sdp.right.entity.SysDictItem;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2023-10-11
 */
@RestController
@RequestMapping("/SysDictItemController")
public class SysDictItemController {

	@Autowired
	private Query query;
	
	@Autowired
	private ISysDictItemService	iSysDictItemService;
	
	@Autowired
	private RightCleanCache RightCleanCache;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iSysDictItemService, SysDictItem.class, dataCenter));
	}
	
	@GetMapping(value = "/del")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> del(@RequestParam(name="keyId",required=true) String keyId,@RequestParam(name="dictCode",required=true) String dictCode) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("字典信息不存在，不能删除");
		}
		iSysDictItemService.removeById(keyId);
		//删除缓存
		RightCleanCache.cleanSysDictItemCache(dictCode);
		return Result.ok();
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody SysDictItem sysDictItem) {
		if(sysDictItem==null) {
			return Result.error("字典数据为空");
		}
		if(StringUtils.isEmpty(sysDictItem.getKeyId())) {
			iSysDictItemService.save(sysDictItem);
		}else {
			iSysDictItemService.updateById(sysDictItem);
		}
		//删除缓存
		RightCleanCache.cleanSysDictItemCache(sysDictItem.getDictCode());
		return Result.ok();
	}
	
	
}

